package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrOperatingJicaiManageBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.jicai.view.adapter.FactoryCentralizedListItemAdapter2;
import com.sgb.kjwl.viewmodel.manage.JiCaiManageModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.List;
import javax.inject.Inject;

/**
 * Time:2021/5/16
 * AUTHOR:F_Q
 * Description:企业网站-经营业务-集采经营
 */
public class JiCaiManageFragment extends AppLazyFragment {
    @Inject
    JiCaiManageModel viewModel;
    private FrOperatingJicaiManageBinding binding;

    private String websiteNo;
    private int mTypeIndex;
    private FactoryCentralizedListItemAdapter2 gongfangAdapter;
    //是否可以加载更多
    private boolean noMoreData;

    /**
     * 页面传参
     * @param websiteNo  公司no
     * @param position   位置
     * @return
     */
    public static JiCaiManageFragment newInstance(String websiteNo, int position) {
        JiCaiManageFragment fragment = new JiCaiManageFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putInt("TYPE_INDEX", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_operating_jicai_manage, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            mTypeIndex = getArguments().getInt("TYPE_INDEX", 0);
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        initRecycler();
    }

    private void initRecycler() {
        binding.listView.setPullRefreshEnabled(true);
        binding.listView.setLoadingMoreEnabled(true);
        binding.listView.setLayoutManager(new LinearLayoutManager(getContext()));
        gongfangAdapter = new FactoryCentralizedListItemAdapter2(getContext(), null,"2");
        binding.listView.setAdapter(gongfangAdapter);
        binding.listView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });
        gongfangAdapter.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity entity, int position) {
                viewModel.getH5Url(entity.getNo());
            }
        });
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getGongFangFaQiList(websiteNo, isLoadMore);
    }

    @Override
    protected void onLazyLoad() {
        initFilterData(false);
    }

    //集采-供方发起列表
    public void refreshRecycler(List<FactoryCentralizedItemEntity> list, boolean loadMore, boolean noMoreData) {
        binding.listView.refreshComplete();
        binding.listView.loadMoreComplete();
        binding.listView.setNoMore(noMoreData);
        if (loadMore) {
            if (list != null && list.size() > 0) {
                gongfangAdapter.addDatas(list);
            }else {
                binding.listView.setNoMore(true);
            }
        } else {
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.listView.setVisibility(View.VISIBLE);
                gongfangAdapter.setDatas(list);
                binding.listView.setNoMore(false);
            } else {
                gongfangAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.listView.setVisibility(View.GONE);
            }
        }
    }

    //跳转到详情界面
    public void showUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }
}
